package com.ldk.framework.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ldk.base.framework.dao.IBaseDAO;
import com.ldk.base.framework.service.impl.BaseService;
import com.ldk.base.framework.util.SystemUtil;
import com.ldk.framework.dao.ICashApplyDAO;
import com.ldk.framework.entity.CashApply;
import com.ldk.framework.entity.CashRecord;
import com.ldk.framework.entity.Driver;
import com.ldk.framework.entity.PlatformFundFlowRecord;
import com.ldk.framework.entity.TicketUser;
import com.ldk.framework.entity.User;
import com.ldk.framework.enums.CashApplyStatus;
import com.ldk.framework.enums.FlowInOut;
import com.ldk.framework.enums.FlowPurpose;
import com.ldk.framework.enums.PayType;
import com.ldk.framework.enums.PlatformUserType;
import com.ldk.framework.service.ICashApplyService;
import com.ldk.framework.service.ICashRecordService;
import com.ldk.framework.service.IDriverService;
import com.ldk.framework.service.IPlatformFundFlowRecordService;
import com.ldk.framework.service.ITicketUserService;
import com.ldk.framework.service.IUserService;

/**
 * CashApplyService接口实现类
 *
 * @author xiangwl
 */
@Service
public class CashApplyService extends BaseService<CashApply> implements ICashApplyService {

    @Autowired
    private IUserService userService;
    @Autowired
    private IDriverService driverService;
    @Autowired
    private ICashRecordService recordService;
    @Autowired
    private ITicketUserService ticketUserService;
    @Autowired
    private IPlatformFundFlowRecordService platformFundFlowRecordService;

    @Autowired
    private ICashApplyDAO cashApplyDAO;

    @Override
    protected IBaseDAO<CashApply> getBaseDao() {
        return this.cashApplyDAO;
    }

    @Override
    @Transactional
    public Boolean submitAudit(String dbKey, CashApply cashApply, String operator) {
        //判断申请是否存在
        if (cashApply == null) return false;
        CashApply entity = new CashApply();
        entity.setApplyId(cashApply.getApplyId());
        entity.setStatus(cashApply.getStatus());
        entity.setCashTime(SystemUtil.getAllSeconds());
        entity.setOperator(operator);

        int row = updateByKey(dbKey, entity);


        //操作成功且是为审核通过
        if (row > 0) {
            int userType = cashApply.getApplyUserType();
            String openId = cashApply.getApplyUser();
            Double applyCash = cashApply.getApplyCash();
            String userTypeName = "未知";

            java.lang.Double beforeBalance = 0.0;
            java.lang.Double afterBalance = 0.0;

            //用户申请提现审核
            if (userType == PlatformUserType.CLIENT_USER.getValue()) {
                User user = userService.getUserByOpenId(dbKey, openId);
                //如果用户存在且余额大于申请金额
                if (user != null && user.getBalance().compareTo(applyCash) == 1) {
                    beforeBalance = user.getBalance() == null ? 0 : user.getBalance();
                    //提现后的余额＝之前的余额－申请提现金额
                    afterBalance = beforeBalance - applyCash;

                    if(cashApply.getStatus() == CashApplyStatus.Success.getValue()){
                        User userEntity = new User();
                        userEntity.setUserId(user.getUserId());
                        userEntity.setBalance(afterBalance);
                        userService.updateByKey(dbKey, userEntity);
                    }
                }
                userTypeName = "用户";
            }
            //服务人员申请提现审核
            if (userType == PlatformUserType.DRIVER_USER.getValue()) {
                Driver driver = driverService.getDriverByOpenId(dbKey, openId);
                //如果服务人员存在且余额大于申请提现金额
                if (driver != null && driver.getBalance().compareTo(applyCash) == 1) {
                    beforeBalance = driver.getBalance();
                    //提现后的余额＝之前的余额－申请提现金额
                    afterBalance = beforeBalance - applyCash;

                    if(cashApply.getStatus() == CashApplyStatus.Success.getValue()){
                        //司机提现金额
                        double cash = driver.getCash() == null ? 0 : driver.getCash();
                        //总提现金额
                        cash = cash + applyCash;
                        Driver staffEntity = new Driver();
                        staffEntity.setDriverId(driver.getDriverId());
                        staffEntity.setBalance(afterBalance);
                        staffEntity.setCash(cash);
                        driverService.updateByKey(dbKey, staffEntity);
                    }
                }
                userTypeName = "司机";
            }
            //票务经理申请提现审核
            if (userType == PlatformUserType.TICKET_USER.getValue()) {
                TicketUser ticketUser = ticketUserService.getTicketInfoByPhoneNo(dbKey, openId);
                //如果服务人员存在且余额大于申请提现金额
                if (ticketUser != null) {
                    beforeBalance = ticketUser.getBalance();
                    //提现后的余额＝之前的余额－申请提现金额
                    afterBalance = beforeBalance - applyCash;
                    if(cashApply.getStatus() == CashApplyStatus.Success.getValue()){
                        //司机提现金额
                        double cash = ticketUser.getCash() == null ? 0 : ticketUser.getCash();
                        //总提现金额
                        cash = cash + applyCash;
                        TicketUser staffEntity = new TicketUser();
                        staffEntity.setId(ticketUser.getId());
                        staffEntity.setPhoneNo(ticketUser.getPhoneNo());
                        staffEntity.setBalance(afterBalance);
                        staffEntity.setCash(cash);
                        ticketUserService.updateByKey(dbKey, staffEntity);
                    }
                }
                userTypeName = "票务经理";
            }

            entity.setApplyId(cashApply.getApplyId());
            entity.setStatus(cashApply.getStatus());
            entity.setCashTime(SystemUtil.getAllSeconds());
            entity.setOperator(operator);
            entity.setBeforeBalance(beforeBalance);
            entity.setAfterBalance(afterBalance);
            updateByKey(dbKey, entity);
            if (cashApply.getStatus() == CashApplyStatus.Success.getValue()) {

                //添加提现记录
                String recordNo = SystemUtil.generateRandomNo("");
                CashRecord record = new CashRecord();
                record.setRecordNo(recordNo);
                record.setOpenId(openId);
                record.setUserType(userType);
                record.setCash(applyCash);
                record.setBeforeBalance(beforeBalance);
                record.setAfterBalance(afterBalance);
                record.setCashTime(SystemUtil.getAllSeconds());
                record.setOperator(operator);
                recordService.insert(dbKey, record);
                //增加平台流动资金记录
                PlatformFundFlowRecord flowRecord = new PlatformFundFlowRecord();
                flowRecord.setRecordNo(SystemUtil.generateRandomNo(""));
                flowRecord.setUserUniqueId(openId);
                flowRecord.setFlowAmount(applyCash);
                flowRecord.setInOut(FlowInOut.OUT.getValue());
                flowRecord.setFlowFrom("平台余额");
                flowRecord.setFlowTo(userTypeName + "余额");
                //提现是余额
                flowRecord.setFlowMode(PayType.Balance.getId());
                //提现对应提现记录的编号
                flowRecord.setLinkNo(recordNo);
                String purpose = userType == PlatformUserType.CLIENT_USER
                        .getValue() ? FlowPurpose.USER_CASH.getName()
                        : FlowPurpose.DRIVER_CASH.getName();
                flowRecord.setPurpose(purpose);
                platformFundFlowRecordService.insert(dbKey, flowRecord);
            }
        }

        return true;
    }

}
